ZK Testing with Sahi
Enrico Tedeschini
October 15, 2013
ZK 6.5 (or latest)
Introduction
This article describes how it’s easy to test your ZK web application with Sahi. Sahi offers a completely different solution from ZATS. ZATS is a powerful solution to test a pure web application functionality and incredible product for Automate Unit Tests without server and browser. However, if you want to test your ZK web application in a real environment with different browsers, different servers and different network traffic/latency you have to use a tool like Sahi. I tried different tools like Selenium, JMeter and others but all of them require changing code. They require to create a Custom ID Generator as described in this article Perform stress test on ZK using JMeter-take Shopping Cart as an example.
All tests described here are performed with a Sahi Pro Version. But all of them work as well with Sahi Open Source Version. From my point of view Sahi is a powerful tool for regression, stress, reliable and performing tests for any ZK web applications. For example Sahi can help you in the troubleshooting process. When you see a problem you only have to, through Sahi Dashboard, record all steps and playback them later. All of this without writing any code or having any particular skill. Sahi works on Windows and Linux platform as well. In this article I described how to test a ZK web application on Windows platform.
Environment
Microsoft Windows 7 platform with
- Firefox Version 22.x
- Chrome Version 29.x
- Internet Explorer Version 8.x
- Safari Version 5.1.x
- JRE 6 or later
- Apache Tomcat 6.x or other Web Server or Application Server
Steps
Download and Install Sahi
- Download Sahi Pro Trial Version. Go to http://sahi.co.in/sahi-pro/, press Free Trial button and fill the form to download a fully functional version of Sahi Pro V5.0.9, valid for 1 month. The license key and a download link will be emailed to you.
- Install Sahi Pro wherever you want. In this example Sahi Pro was installed on C:/workspace/sahi_pro directory. It is recommended to install Sahi in a folder without spaces in the path. You don’t need to change any other setting. Press Next button until the end where you have to press, Done to complete the installation.
- Copy the license.data file received by email on C:/workspace/sahi_pro/userdata/config directory
Configure Sahi
Start Sahi Dashboard by double click on the desktop shortcut.
Then press the Configure hyperlink to make visible on the Dashboard all browsers installed on your computer. Browsers’ path should match the browser location on your computer. When all browsers location match you can see a Sahi Dashboard like this.
Install ZK sample project
To perform these tests it was created an essential ZK web application project.
This is the web application directory structure (Source code files included)
ExampleContacts - WebContent/
images/
add_contact.png
edit_contact.png
refresh_contacts.png
remove_contact.png
licenses
COPYING
COPYING.LESSER
COPYING.zk_libraries
META-INF/
MANIFEST.MF
WEB-INF/
classes/
contacts/
Contacts.class
Contacts.java
ContactsContainer.class
ContactsContainer.java
ContactsContainer.class
ContactsContainer.java
ViewCntroller$1.class
ViewCntroller$2.class
ViewCntroller$3.class
ViewCntroller$4.class
ViewController$Predicate.class
ViewController.class
ViewController.java
lib/
ZK CE 6.5.x libraries
web.xml
zk.xml
edit.zul
index.zul
timeout.zul
view.zul
And when you run this ZK web application the result is as follows:
To do this follow the instructions below:
- Download the ExampleContacts.war (Web application archive).
The ExampleContacts is hosted on github here. Note that the use of ExampleContacts is free for non-profit organisations, students, universities, public schools and non-commercial personal websites. - Deploy this ZK web application in your web server or application server.
- Open a Browser instance from Sahi Dashboard. Go to the navigation bar, write
http://localhost:8080/ExampleContacts/ and press Enter.
The above html page will appear otherwise do not forget to verify the right hostname and port in case you have a different server configuration.
Create a Sahi script
So, now you have to create your first Sahi script.
To do this follow these instructions:
- Create a demo.sahi with this content
// Script to test ExmapleContacts web application //-------------------------------------------------------- _assertExists(_cell("Add")); // check if exist 'Add' button _assertExists(_cell("Edit")); // check if exist 'Edit' button _assertExists(_cell("Remove")); // check if exist 'Remote' button _assertExists(_cell("Refresh")); // check if exist 'Refresh' button _setValue(_textbox(0, _near(_span("Search:"))), ""); // Clear input filter _click(_cell("Refresh")); // Press 'Refresh' button // Verify if exist "Camille Bauer" contact if (_exists(_div("Camille Bauer"))) { _click(_click(_div("Camille Bauer"))); // select "Camille Bauer" row _click(_cell("Edit")); // edit contact _wait(2000); // wait few seconds just to see the dialog box _click(_cell("Cancel")); // press Cancel and exit from dialog box } // Verify if exist "Benedict Ewing" contact if (_exists(_div("Benedict Ewing"))) { _click(_div("Benedict Ewing")); // select "Benedict Ewing" row _click(_cell("Remove")); // remove "Benedict Ewing" contact _wait(2000); // wait few seconds just to see the dialog box _click(_cell("OK")); // press OK to confirm } _click(_image("add_contact.png")) // Press 'Add' button to open 'Add Contact' form // fill input fields _setValue(_textbox(0, _near(_span("Name *"))), "Benedict Ewing"); _setValue(_textbox(0, _near(_span("Address"))), "3393 Gravida. St."); _setValue(_textbox(0, _near(_span("Country"))), "United Kingdom"); _setValue(_textbox(0, _near(_span("Phone"))), "+44 605 4296-9504"); _setValue(_textbox(0, _near(_span("Email"))), "ornare.sagittis@aliquetlobortis.net"); // Move cursor between input fields - fix IE and ZK constraints problem if (_isIE()) { _focus(_textbox(0, _near(_span("Name *")))); _focus(_textbox(0, _near(_span("Address")))); } _click(_cell("OK")); // press OK to confirm '"Benedict Ewing' as new contact _setValue(_textbox(0, _near(_span("Search:"))), "Benedict"); // Fill input filter with new contact's name _keyPress(_textbox(0, _near(_span("Search:"))), [13, 0]) // Press enter to confirm
- Copy this script in C:/workspace/sahi_pro/userdata/scripts directory
This sample script performs different operations:
- Assertions check,
- navigation,
- fill input fields
- and execution of simple actions
Enough to understand how Sahi works.
Or through Sahi Controller you can record your scripts. For this follow the instructions in this document - see chapter Recording through Sahi
Running test from Sahi controller
Now we are ready to start the browser and to run the test.
- From Sahi Dashboard launch IE browser or Firefox browser or Chrome browser
- Press the ALT key and Double Click on page to bring up the Sahi Controller
- Click on Playback tab in the Sahi Controller
- Fill out demo.sah in the File field
- Fill out http://localhost:8080/ExampleContacts/ in the Start URL field
- Click Set button and wait until the page http://localhost:8080/ExampleContacts/ is loaded or reloaded
- Click Play button to run the test
- Wait until all steps are performed. SUCCESS or FAILURE message will be displayed at the end of the steps.
Click on Log link at the bottom of Sahi Controller to see all test details in a HTML report page.
Running tests in sequence from batch file
To run a single test with Firefox, Chrome, Internet Explorer and Safari in sequence you have to follow this instruction:
- Create a demo.bat batch file with this four lines wherever you want
cd C:\Workspace\sahi_pro\userdata\bin call testrunner.bat "demo.sah" "http://localhost:8080/ExampleContacts/" "firefox" call testrunner.bat "demo.sah" "http://localhost:8080/ExampleContacts/" "chrome" call testrunner.bat "demo.sah" "http://localhost:8080/ExampleContacts/" "ie" call testrunner.bat "demo.sah" "http://localhost:8080/ExampleContacts/" "safari"
- Verify Sahi Dashboard running
- Close all browser instances
- Launch demo.bat to run the test.
- Wait until all steps are performed.
Click on Log link at the bottom of Sahi Dashboard to see all test details in a HTML report page.
Running tests in parallel from batch file
Sahi allows parallel or multithreaded playback. Multiple tests can be run simultaneously and you can also spread the tests across machines. To run three simultaneously test with Firefox you have to follow this instruction:
- Create a demo.suite file with these three lines.
demo.sah demo.sah demo.sah
Each line will be running simultaneously - Copy demo.suite in in C:/workspace/sahi_pro/userdata/scripts directory
- Create a demo_suite.bat batch file with these lines wherever you want
cd C:\Workspace\sahi_pro\userdata\bin call testrunner.bat "demo.suite" "http://localhost:8080/ExampleContacts/" "firefox"
- Verify Sahi Dashboard running
- Close all browser instances
- Launch demo_suite.bat to run the test.
- Wait until all steps are performed.
Click on Log link at the bottom of Sahi Dashboard to see all test details in a HTML report page.
Troubleshooting ID Generator
As stated in the Introduction, Sahi does not require creating a Custom ID Generator.
In your script you have to identify your element with _near and _in functions.
This image shows the Edit Contact dialog box. Suppose you have to fill the Country field with United Kingdom. To identify the input field and fill it you have different solutions.
Solution 1 (WRONG): Write
_setValue(_textbox(X), "United Kingdom");
Where X is the ZK component's UUID. As we know, ZK by default generate random component's ID for preventing Cross-Site Request Forgery. Reason why this solution it’s not feasible.
Solution 2 (RIGHT): Write
_setValue(_textbox(0, _near(_span("Country"))), "United Kingdom");
Where the _near function identify the Country input field by the nearest DOM Country label. With this solution all problems related to the ZK ID are solved.
Using _near or _in functions is a simple and powerful solution when DOM elements are not uniquely identifiable by themselves.
Summary
This Small Talk is created as a simple demonstration of Sahi and ZK. However, if you want to use Sahi in a more advanced way with more complex functions, you may consider to read this powerful documentation http://sahi.co.in/w/file_download/5/SahiPro_V4_2_Documentation.pdf.
Do not forget to read about the advanced way of automation tests via its Excel Framework and Ant scripts.
Comments
Copyright © Potix Corporation. This article is licensed under GNU Free Documentation License. |